package com.develop.web.in.service;

//不同产品有不同的实现，不同的计算分期的方式

import com.develop.data.loan.manager.change.amount.api.LoanInstallmentAmountBudget;
import com.develop.data.loan.model.LoanInstallment;
import com.develop.web.in.service.api.LoanInstallmentBudgetService;
import com.develop.web.in.service.api.LoanInstallmentDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.money.MonetaryAmount;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;

@Component
public class L0001B0001LoanInstallmentBudgetService implements LoanInstallmentBudgetService<LoanInstallment,LoanInstallment> {

    @Autowired
    @Qualifier("LoanInstallmentJpaService")
    LoanInstallmentDetailService<LoanInstallment, LoanInstallment> loanInstallmentDetailService;

    /*利息*/
    @Autowired
    @Qualifier("L0001B0001LoanInstallmentInterestBudget")
    LoanInstallmentAmountBudget loanInstallmentInterestBudget;

    /*逾期利息*/
    @Autowired
    @Qualifier("L0001B0001LoanInstallmentInterestOverdueBudget")
    LoanInstallmentAmountBudget loanInstallmentInterestOverdueBudget;


    /*本金*/
    @Autowired
    @Qualifier("L0001B0001LoanInstallmentPrincipleBudget")
    LoanInstallmentAmountBudget loanInstallmentPrincipleBudget;


    /*逾期本金*/
    @Autowired
    @Qualifier("L0001B0001LoanInstallmentPrincipleOverdueBudget")
    LoanInstallmentAmountBudget loanInstallmentPrincipleOverdueBudget;

    /*其他......*/


    @Override
    public List<LoanInstallment> batchPreview(List<LoanInstallment> items) {

        /*
         * 预算当期本金、逾期本金
         * 预算当前利息、逾期利息、罚息
         */

        List<LoanInstallment> targets = loanInstallmentDetailService.batchDetail(items);

        IntStream.range(0, items.size()).forEach(i -> {

            //获取传入的分期ID
            LoanInstallment item = items.get(i);
            //获取传入的动帐日期
            LocalDateTime cursor = item.getInstallmentCursor();
            //根据分期ID查询到的分期
            LoanInstallment target = targets.get(i);

            MonetaryAmount interestAccrual = loanInstallmentInterestBudget.preview(target, cursor);
            target.setInstallmentInterestAccrual(interestAccrual);

            MonetaryAmount interestOverdue = loanInstallmentInterestOverdueBudget.preview(target, cursor);
            target.setInstallmentInterestOverdue(interestOverdue);

        });

        return targets;
    }

    public LoanInstallment preview(LoanInstallment item) {

        /*
         * 预算当期本金、逾期本金
         * 预算当前利息、逾期利息、罚息
         */

        //获取传入的动帐日期
        LocalDateTime cursor = item.getInstallmentCursor();
        //根据分期ID查询到的分期
        LoanInstallment target = loanInstallmentDetailService.detail(item);

        MonetaryAmount interestAccrual = loanInstallmentInterestBudget.preview(target, cursor);
        target.setInstallmentInterestAccrual(interestAccrual);

        MonetaryAmount interestOverdueAccrual = loanInstallmentInterestOverdueBudget.preview(target, cursor);
        target.setInstallmentInterestOverdue(interestOverdueAccrual);

        MonetaryAmount principalAccrual = ;





        return target;
    }


}
